package com.dhj.streamTest2;

import com.dhj.streamTest.entity.Author;

import java.util.Comparator;
import java.util.List;
import java.util.Optional;

import static com.dhj.streamTest.entity.AuthorList.getAuthors;

public class Demo3 {

    public static void main(String[] args) {
        List<Author> authors = getAuthors();

        Optional<Integer> max = authors.stream().distinct()
                .flatMap(author -> author.getBooks().stream())
                .map(book -> book.getScore())
                .max(new Comparator<Integer>() {
                    @Override
                    public int compare(Integer o1, Integer o2) {
                        return o1 - o2;
                    }
                });

        System.out.println(max.get());


        Optional<Integer> min = authors.stream().distinct()
                .flatMap(author -> author.getBooks().stream())
                .map(book -> book.getScore())
                .min(new Comparator<Integer>() {
                    @Override
                    public int compare(Integer o1, Integer o2) {
                        return o1 - o2;
                    }
                });

        System.out.println(min.get());


    }
}
